﻿package com.example.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.example.taigame.R;

enum DialogType {
	Ok, OkCancel, YesNo, YesNoCancel
}

public class MoboDialog extends Dialog {

	private TextView _tvAlertTitle;
	private TextView _tvAlertMessage;

	public static final int BUTTON_OK = 2001;
	public static final int BUTTON_CANCEL = 2002;
	DialogType type;
	TextView _btnOk;
	TextView _btnCancel;
	String message, title;
	View _tvMoboDialogButtonSeperation;

	public MoboDialog(Context context) {
		super(context, R.style.Theme_Transparent);
		// Android 3.x+ still wants to show title: disable
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.mobo_dialog);

		// Create our view
		_btnOk = (TextView) findViewById(R.id.btnMoboDialog1);
		_btnOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				MoboDialog.this.dismiss();
			}
		});
		_btnCancel = (TextView) findViewById(R.id.btnMoboDialog2);
		_btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				MoboDialog.this.dismiss();
			}
		});

		_tvAlertMessage = (TextView) findViewById(R.id.tvDialogMessage);
		_tvAlertTitle = (TextView) findViewById(R.id.tvMoboDialogTitle);
		_tvMoboDialogButtonSeperation = (TextView) findViewById(R.id.tvMoboDialogButtonSeperation);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// // Android 3.x+ still wants to show title: disable
		// getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		// setContentView(R.layout.mobo_dialog);
		//
		// // Create our view
		// // View view = inflater.inflate(R.layout.mobo_dialog, container,
		// true);
		// _btnOk = (TextView) findViewById(R.id.btnMoboDialog1);
		// _btnOk.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // Button_1_Click(v);
		// MoboDialog.this.dismiss();
		// }
		// });
		// // _btnCancel.setText(Utils.isNullOrEmpty(buttonText1) ? "OK" :
		// // buttonText1);
		//
		// _btnCancel = (TextView) findViewById(R.id.btnMoboDialog2);
		// _btnCancel.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // Button_2_Click(v);
		// MoboDialog.this.dismiss();
		// }
		// });
		// // _btnCancel.setText(Utils.isNullOrEmpty(buttonText2) ? "Hủy" :
		// // buttonText2);
		//
		// _tvAlertMessage = (TextView) findViewById(R.id.tvDialogMessage);
		// // tv.setText(message);
		// // if (!Utils.isNullOrEmpty(title)) {
		// //
		// _tvAlertTitle = (TextView) findViewById(R.id.tvMoboDialogTitle);
		// tv.setText(title);
		// }
		// if (action2 == null) {
		// btn1.setTextColor(this.getResources().getColor(R.color.c_blue_1));
		// btn1.setTypeface(null, Typeface.BOLD);
		// btn2.setVisibility(View.GONE);
		// ((TextView) view.findViewById(R.id.tvMoboDialogButtonSeperation))
		// .setVisibility(View.GONE);
		// }
		// return view;
	}

	/**
	 * Sets custom alert title
	 * 
	 * @param alertTitle
	 */
	public MoboDialog setTitle(final String alertTitle) {
		_tvAlertTitle.setText(alertTitle);
		return this;
	}

	/**
	 * Sets custom alert message
	 * 
	 * @param alertMessage
	 */
	public MoboDialog setMessage(final String alertMessage) {
		_tvAlertMessage.setText(alertMessage);
		return this;
	}

	/**
	 * Shows dialog with custom message
	 * 
	 * @param message
	 */
	public void show(final String message) {
		_tvAlertMessage.setText(message);
		MoboDialog.this.show();
	}

	/**
	 * Shows dialog with custom message
	 * 
	 * @param message
	 */
	public void showOnlyOK(final String message) {
		_tvAlertMessage.setText(message);
		_btnCancel.setVisibility(View.GONE);
		_tvMoboDialogButtonSeperation.setVisibility(View.GONE);
		this.show();
	}

	/**
	 * Sets new OnClickListener for button "OK" or "CANCEL"
	 * 
	 * @param button
	 *            MoboDialog.BUTTON_OK or MoboDialog.BUTTON_CANCEL
	 * @param onClickListener
	 */
	public void setOnButtonClickListener(final int button, final View.OnClickListener onClickListener) {
		switch (button) {
		case BUTTON_OK:
			_btnOk.setOnClickListener(onClickListener);
			break;
		case BUTTON_CANCEL:
			_btnCancel.setOnClickListener(onClickListener);
			break;
		default:
			break;

		}
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		Log.v("Dialog focus change", "" + hasFocus);
		if (!hasFocus)
			this.dismiss();
		super.onWindowFocusChanged(hasFocus);
	}

}
